package com.woniu108.order.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.order.client.PersonClient;
import com.woniu108.order.client.ProductClient;
import com.woniu108.order.mapper.OrderServiceMapper;
import com.woniu108.order.model.dto.OrderServiceDto;
import com.woniu108.order.model.dto.OrderServiceInfoDto;
import com.woniu108.order.model.model.OrderService;
import com.woniu108.order.service.OrderProductService;
import com.woniu108.order.service.OrderServiceService;
import com.woniu108.product.form.PutStorageForm;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Lenovo
 */
@Component
@RabbitListener(queues = "order_service_del_queue")
public class OrderServiceDelConsumer {
    @Resource
    private ProductClient productClient;
    @Resource
    private OrderServiceService orderServiceService;
    @Resource
    private OrderServiceMapper orderServiceMapper;
    @Resource
    private PersonClient personClient;

    @RabbitHandler
    public void DeleteOrderServiceByNum(String orderServiceNum){
        OrderServiceDto orderServiceDto = orderServiceService.selectOrderServiceByOrderServiceNum(orderServiceNum);
        OrderService orderService = orderServiceMapper.selectById(orderServiceNum);

        if(orderService.getOrderServiceState().equals(StaticData.ORDER_SERVICE_STATE_NO_PAID)){
            //订单执行取消
            orderService.setOrderServiceState(StaticData.ORDER_SERVICE_STATE_CANCEL);
            orderServiceMapper.updateById(orderService);

            //因不需要，服务订单所使用的商品材料归库
            for (OrderServiceInfoDto orderServiceInfoDto:orderServiceDto.getOrderServiceInfoDtos()){
                PutStorageForm putStorageForm = new PutStorageForm();
                putStorageForm.setNum(orderServiceInfoDto.getNum());
                putStorageForm.setSpInfoId(orderServiceInfoDto.getSpInfoId());
                productClient.putStorageTwo(putStorageForm);
            }
            //减除积分
            personClient.reduceUserScore(orderServiceDto.getUserId(),Integer.valueOf(orderServiceDto.getOrderPrice().toString()),StaticData.SCORE_SHOPPING);
        }
    }
}
